#pragma once
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <string>
#include <iostream>
#include <unistd.h>
using namespace std;
namespace sxj{

    static const uint16_t gport = 8080;
    static const int gbacklog = 5;
    class tcpserver
    {
        public:
            tcpserver(const uint16_t &port = gport):_listensock(-1),_port(port)
            {}

            void initServer()
            {
                //创建套接字
                _listensock = socket(AF_INET,SOCK_STREAM,0);
                if(_listensock < 0)
                {
                    cerr<<"create socket error!"<<endl;
                    exit(-1);
                }
                cout<<"create socket success!"<<endl;

                //bind进行绑定
                struct sockaddr_in local;
                memset(&local,0,sizeof(local));

                local.sin_family = AF_INET;
                local.sin_port = htons(_port);
                local.sin_addr.s_addr = INADDR_ANY;
                if(bind(_listensock,(struct sockaddr*)&local,sizeof local) < 0)
                {
                    cerr<<"bind socket error"<<endl;
                    exit(-2);
                }
                cout<<"bind socket success"<<endl;

                //设置socket为监听状态
                if(listen(_listensock,gbacklog) < 0) //第二个参数 backlog 之后再学
                {
                    cerr<<"listen socket error!"<<endl;
                    exit(-3);
                }

                cout<<"listen socket success"<<endl;

            }

            void start()
            {
                for(;;)
                {
                    struct sockaddr_in peer;
                    socklen_t len= sizeof peer;
                    int sock = accept(_listensock,(struct sockaddr*)&peer,&len);
                    if(sock  < 0)
                    {
                        cerr<<"accpet error!"<<endl;
                        continue;
                    }

                    cout<<"accept success!"<<endl;
                    cout<<"sock: "<< sock << endl;
                    
                    //version 1
                    serviceIO(sock);

                    close(sock); //对于一个已经使用完毕的sock 我们要关闭这个文件描述符 会出现文件描述符泄露
                }

            }

            void serviceIO(int sock)
            {
                char buffer[1024];
                while(1)
                {
                    size_t n = read(sock,buffer,sizeof(buffer)-1);
                    if(n>0)
                    {
                        //目前我们把读到的字符都当成字符串
                        buffer[n] = 0;
                        std::cout<<"recv message:>"<< buffer << std::endl;

                        string outbuffer = buffer;
                        outbuffer += "  server[echo]";
                        write(sock,outbuffer.c_str(),outbuffer.size());
                    }
                    else if(n == 0)
                    {
                        //代表client退出
                        std::cout<<"client quit,my quit too!"<<std::endl;
                        exit(-1);
                    }
                }
            }
        private:
            int _listensock; //不是用来通讯的，而是用来建立连接
            uint16_t _port;
    };




}